import 'dart:convert';

import 'package:http/http.dart';
import 'package:http/http.dart';
import 'package:robotcontrol/http/url.dart';

class HttpManager {
  static Utf8Decoder utf8decoder = Utf8Decoder();

  static void getData(String url,
      {Map<String, String> headers = Url.httpHeader,
        Success success,
        Fail fail,
      Function complete}) async {
    try{
    var response = await get(Uri.parse(url),headers: headers);
    if (response.statusCode == 200){
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      if(null != success)
        success(result);
    }
    }catch(e){
      if(null != fail)
        fail(e);
    }
  }
  static Future requestData(String url, {Map<String, String> headers}) async {
    try {
      var response = await get(Uri.parse(url), headers: headers);
      if (response.statusCode == 200) {
        var result = json.decode(utf8decoder.convert(response.bodyBytes));
        return result;
      } else {
        throw Exception('"Request failed with status: ${response.statusCode}"');
      }
    } catch (e) {
      Future.error(e);
    }
  }

}

typedef void Success<T>(T t);
typedef Fail(e);
